package com.aiju.cash.ui.good.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.cash.ui.good.fragment.GoodFragment;
import com.aiju.cash.ui.good.manager.GoodManager;

/**
 * @author: 佐为
 * @date: 2016-12-13 10:31
 * @version: V1.0
 * @Description: 商品分类
 */
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements GoodManager.OnChangeListener {

    //显示类型 （删除，购物车）
    private int showType;

    //上下文Context
    private Context context;

    //商品管理类
    private GoodManager goodManager;

    /**
     * 构造器
     *
     * @param context
     * @param goodManager
     */
    public CategoryAdapter(Context context, GoodManager goodManager) {
        this.context = context;
        this.goodManager = goodManager;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_category, null));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        GoodsType goodsType = goodManager.getGoodsTypes().get(position);
        holder.textView.setText(goodsType.getName());
        holder.textView.setSelected(goodsType.isSeleted());
        TextPaint paint = holder.textView.getPaint();
        if (goodsType.isSeleted()) {
            paint.setFakeBoldText(true);
        } else {
            paint.setFakeBoldText(false);
        }
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });

        if (showType == GoodFragment.DELETE) { //删除
            if (goodsType.getSelectGoods().size() != 0) {
                holder.textViewTip.setVisibility(View.VISIBLE);
                holder.textViewTip.setText("" + goodsType.getSelectGoods().size());
            } else {
                holder.textViewTip.setText("");
                holder.textViewTip.setVisibility(View.GONE);
            }
        } else if (showType == GoodFragment.SHOP) { //开单购物车
            int count = goodManager.getSelectedGroupCountByTypeId(goodsType.getIdInt());
            if (count != 0) {
                holder.textViewTip.setVisibility(View.VISIBLE);
                holder.textViewTip.setText("" + count);
            } else {
                holder.textViewTip.setText("");
                holder.textViewTip.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return goodManager == null ? 0 : goodManager.getGoodsTypes().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private TextView textViewTip;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textview_categoryname);
            textViewTip = (TextView) itemView.findViewById(R.id.tv_tip);
        }
    }

    //显示类型
    public void setShowType(int showType) {
        this.showType = showType;
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @Override
    public void changeView() {
        notifyDataSetChanged();
    }

}
